package Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import Interact.Interactable;
import Interact.Interface;

public class TestPaper implements Interactable{
	/** 未作答 */
	public static final int UNSOLVED = 0;
	/** 已作答未批改完 */
	public static final int UNCORRECT = 1;
	/** 已批改完 */
	public static final int CORRECTED = 2;
	
	/** 状态名 */
	private static String[] stateName = {"未作答", "正在审阅", "审阅完毕"};
	
	/** 试卷状态 */
	protected TestPaperState state = new TestPaperState();
	
	/** 所含试题 */
	protected ArrayList<Question> questions = new ArrayList<Question>();
	
	/**
	 * 得到状态，浅拷贝
	 * 也就是说可以通过它改变本类的状态
	 */
	public TestPaperState getState(){
		return state;
	}
	
	/** 得到题目数量 */
	public int getQuestionCount(){
		return questions.size();
	}
	
	/** 得到所有题目 */
	public Collection<Question> getQuestions(){
		return questions;
	}
	
	/** 删除指定题号之题目，不进行越界检查 */
	public void removeQuestion(int num){
		questions.remove(num);
	}
	
	/** 插入指定题号之题目至最后 */
	public void pushQuestion(Question q){
		questions.add(q);
		q.content.setNumber(questions.size());
	}
	
	/** 插入指定题号之题目至指定位置，不进行越界检查 */
	public void insertQuestion(Question q, int pos){
		questions.add(pos, q);
	}
	
	/** 更新题目，为空自己负责，不进行越界检查 */
	public void updateQuestion(Question q, int num){
		questions.set(num, q);
	}
	
	int mode = Question.ANSWER_MODE;
	
	@Override
	public void setMode(int mode) {
		Iterator<Question> iter = questions.iterator();
		while (iter.hasNext())
			iter.next().setMode(mode);
	}

	@Override
	public void display(Interface out) throws IOException {		
		for (Iterator<Question> iter = questions.iterator(); iter.hasNext();){
			Question q = iter.next();
			out.print("<b>" + q.getContent().getNumber() + ":&nbsp;</b>");
			out.println(q);
			out.print("<br/>");
		}
	}

	@Override
	public String getKey() {
		return "paper" + state.getId();
	}

	@Override
	public void setValueViaInterface(Interface inf) {
		Iterator<Question> iter = questions.iterator();
		while (iter.hasNext())
			iter.next().setValueViaInterface(inf);
	}
	
	public static String getStateName(int stateCode){
		try{
			return stateName[stateCode];
		}
		catch (Exception e){
			return "未定义的状态";
		}
	}
	
	public void calculateScore(){
		int score = 0;
		Iterator<Question> iter = questions.iterator();
		while (iter.hasNext())
			score += iter.next().content.getScore();
		state.setScore(score);
	}
}



